(function(window) {
	function IScrollLoadData(wrapperEl, contentEl, dropTopAction,
			dropBottomAction, params) {
		this.wrapper = wrapperEl;
		this.content = contentEl;
		this.pullDownEl = this.wrapper.querySelector('#scroller-pullDown');
		$(this.pullDownEl).hide();
		this.pullUpEl = this.wrapper.querySelector('#scroller-pullUp');
		$(this.pullUpEl).hide();
		this.scrollerEl = this.wrapper.querySelector('.scroller');
		this.dropTopAction = dropTopAction;
		this.dropBottomAction = dropBottomAction;
		this.params = params;

		this.clickTop_bind = this.clickTop.bind(this);
		this.clickBottom_bind = this.clickBottom.bind(this);

		//this.pullUpLabel_Text = '上翻刷新...';
		//this.pullDownLabel_Text = '下拉刷新...';
		this.pullLoading_Text = '加载中...';
		this.releaseLoading_Text = '松开加载...';
		this.emptyContext_Text = '';
		this.loading_top_flag = false;
		this.loading_bottom_flag = false;
		this.checkIScroll(true);
		this.createIScroll();
	};
	IScrollLoadData.prototype.refresh = function(direct) {
		this.checkIScroll();
		delete this.myScroll.waitLoadTop;
		this.myScroll.refresh();
	}
	IScrollLoadData.prototype.updateContentLen = function(direct) {
		var children = this.content.children;
		if (children.len > this.limit) {
			if (direct == 'bottom') {

			} else {

			}
		}
	}
	IScrollLoadData.prototype.checkIScroll = function(flag) {
		var soh = this.content.offsetHeight + this.pullDownEl.offsetHeight + this.pullUpEl.offsetHeight;
		var woh = this.wrapper.clientHeight;
		var holder = this.scrollerEl.querySelector('.scroller-holder');
		if (soh < woh) {
			if (!holder) {
				var ul = document.createElement('ul');
				ul.className = 'scroller-holder';
				this.scrollerEl.appendChild(ul);
				holder = ul;
			}
			holder.style.height = (woh - soh + 2) + 'px';
		} else if (holder) {
			this.scrollerEl.removeChild(holder);
		}
	}
	IScrollLoadData.prototype.pullDownAction = function() {
		var self = this;
		if (this.dropTopAction) {
			this.dropTopAction(function(out) {
				if(out){
					var firstChild = self.content.querySelector(':first-child');
					if (firstChild)
						$(self.content).append(out, firstChild);
					else
						$(self.content).append(out);					
				}
				self.refresh('top');
			});
		} else {
			this.refresh('top');
		}
	};
	IScrollLoadData.prototype.pullUpAction = function() {
		var self = this;
		if (this.dropBottomAction) {
			this.dropBottomAction(function(out) {
				if(out){				
					$(self.content).append(out);
				}
				self.refresh('bottom');
			});
		} else {
			this.refresh('bottom');
		}
	};
	IScrollLoadData.prototype.clickTop = function() {
		var self = this;
		if (this.dropTopAction) {
			this.dropTopAction(function(out) {
				var firstChild = self.content.querySelector(':first-child');
				if (firstChild)
					self.content.insertBefore(out, firstChild);
				else
					self.content.appendChild(out);
				self.initIScroll(false);

			});
		}
	};
	IScrollLoadData.prototype.clickBottom = function() {
		var self = this;
		if (this.dropBottomAction) {
			this.dropBottomAction(function(out) {
				self.content.appendChild(out);
				self.initIScroll(false);
			});
		}
	};
	IScrollLoadData.prototype.initIScroll = function(flag) {
		if (this.overflow_window) {
			return;
		}
		if (this.scrollerEl.offsetHeight - this.pullDownEl.offsetHeight >= document.documentElement.clientHeight) {
			this.wrapper.style.bottom = '0px';
			this.wrapper.style.top = ((-1) * this.pullDownEl.offsetHeight) + 'px';
			this.pullUpEl.removeEventListener('click', this.clickBottom_bind);
			this.pullDownEl.removeEventListener('click', this.clickUp_bind);
			this.myScroll.refresh();
			$(this.pullUpEl).hide();//.querySelector('.pullUpLabel').innerText = this.pullUpLabel_Text;
			$(this.pullDownEl).hide();//.querySelector('.pullDownLabel').innerText = this.pullDownLabel_Text;
			this.overflow_window = true;
		} else {
			this.wrapper.style.bottom = (document.documentElement.clientHeight - this.scrollerEl.offsetHeight) + 'px';
			this.pullUpEl.querySelector('.pullUpLabel').innerText = '点击获取最近...';
			this.pullDownEl.querySelector('.pullDownLabel').innerText = '点击获取最新...';
			if (flag) {
				this.pullUpEl.addEventListener('click', this.clickBottom_bind);
				this.pullDownEl.addEventListener('click', this.clickTop_bind);
			}

		}
	};
	IScrollLoadData.prototype.createIScroll = function() {
		var self = this;
		this.myScroll = new IScrollLoadData.IScroll(this.wrapper, {
			probeType : 2,
			mouseWheel : false,
			bindToWrapper : true,
			scrollY : true,
			click: true
		}).on('scroll',function() {
			if (this.y > 50 && !self.pullDownEl.className.match('flip')) {
				self.pullDownEl.className = 'flip';
				self.pullDownEl.querySelector('.pullDownLabel').innerHTML = self.releaseLoading_Text;
				this.waitLoadTop = self.pullDownEl.offsetHeight;
			} else if (this.y < 50 && self.pullDownEl.className.match('flip')) {
				self.pullDownEl.className = '';
				$(self.pullDownEl).hide();//.querySelector('.pullDownLabel').innerHTML = self.pullDownLabel_Text;
				delete this.waitLoadTop;
			} else if (this.y < (this.maxScrollY - 5) && !self.pullUpEl.className.match('flip')) {
				self.pullUpEl.className = 'flip';
				self.pullUpEl.querySelector('.pullUpLabel').innerHTML = self.releaseLoading_Text;
			} else if (this.y > (this.maxScrollY + 5) && self.pullUpEl.className.match('flip')) {
				self.pullUpEl.className = '';
				$(self.pullUpEl).hide();//.querySelector('.pullUpLabel').innerHTML = self.pullUpLabel_Text;
			}
		}).on('scrollEnd', function() {
			// 如果滑动到底部，则加载更多数据（距离最底部10px高度）
			if ((this.y - this.maxScrollY) > 10 || this.x - 10 < 0) {
				if (self.pullDownEl.className.match('flip')) {
					$(self.pullDownEl).show();
					self.pullDownEl.className = 'loading';
					self.pullDownEl.querySelector('.pullDownLabel').innerHTML = self.pullLoading_Text;
					self.pullDownAction();// Execute custom
					
				} else if (self.pullUpEl.className.match('flip')) {
					$(self.pullUpEl).show();
					self.pullUpEl.className = 'loading';
					self.pullUpEl.querySelector('.pullUpLabel').innerHTML = self.pullLoading_Text;
					self.pullUpAction(); // Execute custom
				}
			}
		}).on('refresh',function() {
			if (self.pullDownEl.className.match('loading')) {
				self.pullDownEl.className = '';
				if (self.params.hasPreviousPage) {
					$(self.pullDownEl).hide();//.querySelector('.pullDownLabel').innerHTML = self.pullDownLabel_Text;
				} else {
					self.pullDownEl.querySelector('.pullDownLabel').innerHTML = self.emptyContext_Text;
				}
//				$('#scroller-pullDown').hide();
			} else if (self.pullUpEl.className.match('loading')) {
				self.pullUpEl.className = '';
				if (self.params.hasNextPage) {
					$(self.pullUpEl).hide();//.querySelector('.pullUpLabel').innerHTML = self.pullUpLabel_Text;
				} else {
					self.pullUpEl.querySelector('.pullUpLabel').innerHTML = self.emptyContext_Text;
				}
//				$('#scroller-pullUp').hide();
			}
		});

	};
	if ("function" == typeof define && define.amd) {
		define("IScrollLoadData", [ 'IScroll' ], function(IScroll) {
			IScrollLoadData.IScroll = IScroll;
			return IScrollLoadData;
		});
	} else {
		IScrollLoadData.IScroll = window.IScroll;
		window.IScrollLoadData = IScrollLoadData;
	}
})(window);